package cn.wsw.hrm.controller;


import cn.wsw.hrm.util.AjaxResult;
import cn.wsw.hrm.util.FastDfsApiOpr;
import feign.Response;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;

@RestController
@RequestMapping("/fast")
public class FastdfsController {

    //=========================让外部操作===================
    @PostMapping(produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
            , consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult upload(@RequestPart(value = "file",required = true)MultipartFile file){
        try {
            System.out.println(file.getOriginalFilename() + ":" + file.getSize());
            String originalFilename = file.getOriginalFilename();
            // xxx.jpg
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
            System.out.println(extName);
            String filePath =  FastDfsApiOpr.upload(file.getBytes(), extName);
            return AjaxResult.me().setResultObj(filePath);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传失败!"+e.getMessage());
        }
    }


    // /goup1/xxxxx/yyyy
    @DeleteMapping
    public AjaxResult upload(@RequestParam(value = "filePath",required = true)String filePath){
        String pathTmp = filePath.substring(1); // goup1/xxxxx/yyyy
        String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
        String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
        FastDfsApiOpr.delete(groupName, remotePath);
        return  AjaxResult.me();
    }

    //===========================让内部操作==============================
    @PostMapping
//    public AjaxResult upload(@RequestParam(value = "file",required = true)MultipartFile file){
//    };


    // /goup1/xxxxx/yyyy
    @GetMapping
    void download(@RequestParam(value = "filePath",required = true)String filePath, HttpServletResponse response){
        ByteArrayInputStream inputStream=null;
//        ServletOutputStream outputStream=null;
        try {
            //去掉路径开头的  /
            String path = filePath.substring(1);  //   gourp/xxxx/yyyy
            //获取从索引0开头到第一次出现 / 的子字符串  相当于公共路径
            String gourpName = path.substring(0, path.indexOf("/"));//  gourp
            //获取另一半路径
            String fileName = gourpName.substring(path.indexOf("/") + 1); //  xxxx/yyyy
            //获取文件，转换成bety
            byte[] data = FastDfsApiOpr.download(gourpName, fileName);
            //以流的形式做响应
            inputStream = new ByteArrayInputStream(data);
            ServletOutputStream outputStream = response.getOutputStream();
            IOUtils.copy(inputStream,outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            /*if (outputStream!=null){
                try{
                    outputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }*/if (inputStream!=null){
                try{
                    inputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    };

}
